from django.db import models
from product.models import Product
from supplier.models import Supplier
from customer.models import Customer
from employee.models import Employee

class StockIn(models.Model):
    """入库单"""
    stock_in_no = models.CharField(max_length=20, unique=True, verbose_name='入库单号')
    supplier = models.ForeignKey(Supplier, on_delete=models.PROTECT, verbose_name='供应商')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name='总金额')
    status = models.IntegerField(default=0, verbose_name='状态')  # 0:待审核 1:已审核
    create_user = models.ForeignKey(Employee, on_delete=models.PROTECT, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'stock_in'
        verbose_name = '入库单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']

    def __str__(self):
        return self.stock_in_no

class StockInDetail(models.Model):
    """入库单明细"""
    stock_in = models.ForeignKey(StockIn, on_delete=models.CASCADE, related_name='details', verbose_name='入库单')
    product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name='商品')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='入库价格')
    quantity = models.IntegerField(verbose_name='入库数量')
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='金额')

    class Meta:
        db_table = 'stock_in_detail'
        verbose_name = '入库单明细'
        verbose_name_plural = verbose_name

class StockOut(models.Model):
    """出库单"""
    stock_out_no = models.CharField(max_length=20, unique=True, verbose_name='出库单号')
    customer = models.ForeignKey(Customer, on_delete=models.PROTECT, verbose_name='客户')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name='总金额')
    status = models.IntegerField(default=0, verbose_name='状态')  # 0:待审核 1:已审核
    create_user = models.ForeignKey(Employee, on_delete=models.PROTECT, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'stock_out'
        verbose_name = '出库单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']

    def __str__(self):
        return self.stock_out_no

class StockOutDetail(models.Model):
    """出库单明细"""
    stock_out = models.ForeignKey(StockOut, on_delete=models.CASCADE, related_name='details', verbose_name='出库单')
    product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name='商品')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='出库价格')
    quantity = models.IntegerField(verbose_name='出库数量')
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='金额')

    class Meta:
        db_table = 'stock_out_detail'
        verbose_name = '出库单明细'
        verbose_name_plural = verbose_name

class StockRecord(models.Model):
    """库存流水记录"""
    TYPE_CHOICES = (
        (1, '入库'),
        (2, '出库'),
        (3, '盘点'),
        (4, '调拨')
    )
    
    product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name='商品')
    type = models.IntegerField(choices=TYPE_CHOICES, verbose_name='操作类型')
    quantity = models.IntegerField(verbose_name='操作数量')
    before_stock = models.IntegerField(verbose_name='操作前库存')
    after_stock = models.IntegerField(verbose_name='操作后库存')
    related_no = models.CharField(max_length=50, verbose_name='关联单号')
    remark = models.CharField(max_length=200, null=True, blank=True, verbose_name='备注')
    create_user = models.ForeignKey(Employee, on_delete=models.PROTECT, verbose_name='操作人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='操作时间')
    
    class Meta:
        db_table = 'stock_record'
        verbose_name = '库存流水记录'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']
    
    def __str__(self):
        return f'{self.product.name} - {self.get_type_display()} - {self.quantity}'

class StockCheck(models.Model):
    """库存盘点单"""
    STATUS_CHOICES = (
        (0, '待审核'),
        (1, '已审核'),
    )
    
    check_no = models.CharField(max_length=20, unique=True, verbose_name='盘点单号')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name='盈亏金额')
    status = models.IntegerField(choices=STATUS_CHOICES, default=0, verbose_name='状态')
    remark = models.CharField(max_length=200, blank=True, null=True, verbose_name='备注')
    create_user = models.ForeignKey(Employee, on_delete=models.PROTECT, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'stock_check'
        verbose_name = '库存盘点单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']

    def __str__(self):
        return self.check_no

class StockCheckDetail(models.Model):
    """库存盘点单明细"""
    stock_check = models.ForeignKey(StockCheck, on_delete=models.CASCADE, related_name='details', verbose_name='盘点单')
    product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name='商品')
    system_stock = models.IntegerField(verbose_name='系统库存')
    actual_stock = models.IntegerField(verbose_name='实际库存')
    difference = models.IntegerField(verbose_name='差异数量')
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='差异金额')
    remark = models.CharField(max_length=200, blank=True, null=True, verbose_name='备注')

    class Meta:
        db_table = 'stock_check_detail'
        verbose_name = '库存盘点单明细'
        verbose_name_plural = verbose_name

class StockTransfer(models.Model):
    """库存调拨单"""
    STATUS_CHOICES = (
        (0, '待审核'),
        (1, '已审核'),
    )
    
    transfer_no = models.CharField(max_length=20, unique=True, verbose_name='调拨单号')
    from_warehouse = models.CharField(max_length=50, verbose_name='调出仓库')
    to_warehouse = models.CharField(max_length=50, verbose_name='调入仓库')
    status = models.IntegerField(choices=STATUS_CHOICES, default=0, verbose_name='状态')
    remark = models.CharField(max_length=200, blank=True, null=True, verbose_name='备注')
    create_user = models.ForeignKey(Employee, on_delete=models.PROTECT, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'stock_transfer'
        verbose_name = '库存调拨单'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']

    def __str__(self):
        return self.transfer_no

class StockTransferDetail(models.Model):
    """库存调拨单明细"""
    stock_transfer = models.ForeignKey(StockTransfer, on_delete=models.CASCADE, related_name='details', verbose_name='调拨单')
    product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name='商品')
    quantity = models.IntegerField(verbose_name='调拨数量')
    remark = models.CharField(max_length=200, blank=True, null=True, verbose_name='备注')

    class Meta:
        db_table = 'stock_transfer_detail'
        verbose_name = '库存调拨单明细'
        verbose_name_plural = verbose_name 